﻿using DeHeng_Mes.Constant;
using DeHeng_Mes.Dao;
using DeHeng_Mes.Model.OrderSingleton;
using DeHeng_Mes.Model.Table;
using DeHeng_Mes.Utils;
using NPOI.SS.Formula.Functions;
using Org.BouncyCastle.Utilities;
using S7.Net;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace DeHeng_Mes.PlcService
{
    public class ExitService
    {
        #region 成员实例
        OrderDao orderDao = new OrderDao();
        #endregion


        /// <summary>
        /// 执行出站
        /// </summary>
        /// <param name="station"></param>
        /// <returns></returns>
        public (int, List<int>) StationExit(string station, int index, byte[] bytes, string code, string operation)
        {
            // 初始化错误列表
            List<int> errorCodes = new List<int>();

            try
            {
                DateTime now = DateTime.Now;

                switch (station)
                {
                    case SystemConstant.NgStation:

                        // 记录NG工位出站
                        orderDao.NgStationSave(new NgStation()
                        {
                            CreateTime = now,
                            Station = station,
                            TraceId = code,
                            Status = "1"
                        });

                        Tools.ProduceLogText($"[{station}],[{code}]");

                        return (1, errorCodes);
                    default:
                        string result = S7.Net.Types.Int.FromByteArray(bytes.Skip(index * 50 + 4).Take(2).ToArray()) == 1 ? "OK" : "NG";

                        orderDao.UpdateStationEndTime(code, result, station, now, operation);

                        // 绑定工单
                        OrderSingletonModel orderSingletonModel = OrderSingleton.GetOrderId(station);

                        if (orderSingletonModel != null)
                        {

                            bool isSave = orderDao.InsertOrderTrace(orderSingletonModel.OrderId, code, orderSingletonModel.LineName);

                            if (isSave)
                            {
                                Tools.ProduceLogText($"[{orderSingletonModel.OrderId}]触发出站绑定工单，绑定码[{code}]");
                            }
                        }

                        // 判断是否是末工序
                        /*if (station.Equals(SystemConstant.OP40))
                            OrderProduction();*/

                        return (1, errorCodes);
                }
            }
            catch (Exception ex)
            {
                Tools.ProduceLogTextError($"[{station}]触发出站失败，原因是[{ex.Message}]");

                errorCodes.Add(1506);

                return (1, errorCodes);
            }
        }

        /// <summary>
        /// 当前存在工单时首序产量统计
        /// </summary>
        /// <param name="station"></param>
        public void OrderProduction(string station)
        {
            if (!MappingTable.GetStartStation(station))
                return;

            OrderSingletonModel orderSingletonModel = OrderSingleton.Instance.Where(x => x.MaterialId.Equals(MappingTable.GetMaterialByStation(station))).FirstOrDefault();

            if (orderSingletonModel == null)
                return;

            orderDao.UpdateProduction(orderSingletonModel.OrderId);

            OrderSingleton.AddNumNow(orderSingletonModel.OrderId);
        }
    }
}
